/**
 * 
 */
package fr.univ_tln.inf9.exaplanning.api.utilisateur;

import java.io.IOException;
import java.sql.SQLException;

import javax.swing.event.EventListenerList;

import fr.univ_tln.inf9.exaplanning.api.db.AbstractEntity;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.FirstNameChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.FirstNameListener;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.MailChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.MailListener;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.NameChangedEvent;
import fr.univ_tln.inf9.exaplanning.api.utilisateur.event.NameListener;


/**
 * @author pivi
 *
 */
public abstract class User extends AbstractEntity {

	/**
	 * 
	 * @param newObject
	 * @throws IOException
	 * @throws SQLException
	 */
	public User(boolean newObject) throws IOException, SQLException {
		super(newObject);
		listeners = new EventListenerList();
	}

	protected String name;
	protected String first_name;
	protected String mail;
	
	protected EventListenerList listeners;
	

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
		
		fireNameChanged();
	}

	/**
	 * @return the first_name
	 */
	public String getFirst_name() {
		return first_name;
	}

	/**
	 * @param first_name the first_name to set
	 */
	public void setFirst_name(String first_name) {
		this.first_name = first_name;
		
		fireFirstNameChanged();
	}

	

	/**
	 * @return the mail
	 */
	public String getMail() {
		return mail;
	}
	

	/****************************************************************************************************************/
	/****************************************			LISTENER				*************************************/
	/****************************************************************************************************************/

	public void addNameListener(NameListener listener){
		listeners.add(NameListener.class, listener);
	}
	
	public void removeNameListener(NameListener l){
		 listeners.remove(NameListener.class, l);
	}
	
	public void addFirstNameListener(FirstNameListener listener){
		listeners.add(FirstNameListener.class, listener);
	}
	
	public void removeFirstNnameListener(FirstNameListener l){
		 listeners.remove(FirstNameListener.class, l);
	}
	
	
	/**
	 * Méthode pour avertir que le nom de l'utilisateur a changé
	 */
	public void fireNameChanged(){
		NameListener[] listenerList = (NameListener[])listeners.getListeners(NameListener.class);
		
		for(NameListener listener : listenerList){
			listener.NameChanged(new NameChangedEvent(this, getName()));
		}
	}
	
	/**
	 * Méthode pour avertir que le prénom de l'utilisateur a changé
	 */
	private void fireFirstNameChanged() {
		FirstNameListener[] listenerList = (FirstNameListener[])listeners.getListeners(FirstNameListener.class);
		
		for(FirstNameListener listener : listenerList){
			listener.FirstNameChanged(new FirstNameChangedEvent(this, getFirst_name()));
		}
		
	}
	
}
